{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 不推荐共享变量，因为没有锁机制，线程不安全"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from queue import Queue\n",
    "\n",
    "def get_detail_html(queue):\n",
    "    # 如果队列为空，则线程阻塞\n",
    "    url = queue.get()\n",
    "    pass\n",
    "\n",
    "def get_detail_url(queue):\n",
    "    url = queue.get()\n",
    "    pass\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    detail_url_queue = Queue(maxsize=1000)\n",
    "    thread_detail_url = threading.Thread(target=get_detail_url, args=(detail_url_queue, ))\n",
    "    ......\n",
    "    \n",
    "    ## 队列线程退出\n",
    "    detail_url_queue.task_done()\n",
    "    ## 队列线程阻塞\n",
    "    detail_url_queue.join()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Queue的方法 （线程安全）\n",
    "get() 读取队列内容\n",
    "qsize() 获取队列长度\n",
    "empty() 判空\n",
    "join() 队列线程阻塞\n",
    "task_done() 队列线程退出"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
